﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Common;
using Taobao.Top.Api;
using Taobao.Top.Api.Request;
using Taobao.Top.Api.Domain;
using System.Net;
using System.Text;
using System.IO;
using System.Security.Cryptography;
using System.Text.RegularExpressions;
using System.Data;

public partial class top_groupbuy_msgsend : System.Web.UI.Page
{
    public string couponstr = string.Empty;
    public string session = string.Empty;
    public string nick = string.Empty;
    public string itemid = string.Empty;
    public string mindate = string.Empty;
    public string maxdate = string.Empty;
    public string itemstr = string.Empty;
    public string isfree = string.Empty;
    public string iscoupon = string.Empty;
    public string issendmsg = string.Empty;
    public string iskefu = string.Empty;

    public string typ = string.Empty;

    public string count1 = string.Empty;
    public string count2 = string.Empty;
    public string count3 = string.Empty;
    public string count4 = string.Empty;
    public string count5 = string.Empty;
    public string count6 = string.Empty;
    public string count7 = string.Empty;
    public string count8 = string.Empty;
    public string count9 = string.Empty;
    public string groupstr = string.Empty;

    public string mcouponid = string.Empty;
    public string mpage = string.Empty;
    public string mgroupid = string.Empty;
    public string mcount = string.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
        Common.Cookie cookie = new Common.Cookie();
        string taobaoNick = cookie.getCookie("nick");
        session = cookie.getCookie("top_sessiongroupbuy");
        string iscrm = cookie.getCookie("iscrm");
        Rijndael_ encode = new Rijndael_("tetesoft");
        nick = encode.Decrypt(taobaoNick);
        typ = utils.NewRequest("typ", utils.RequestType.QueryString);

        mpage = utils.NewRequest("mpage", utils.RequestType.QueryString);
        mcouponid = utils.NewRequest("mcouponid", utils.RequestType.QueryString);
        mgroupid = utils.NewRequest("mgroupid", utils.RequestType.QueryString);
        mcount = utils.NewRequest("mcount", utils.RequestType.QueryString);
        
        string act = utils.NewRequest("act", utils.RequestType.QueryString);

        //过期判断
        if (string.IsNullOrEmpty(taobaoNick))
        {
            string msg = "尊敬的淘宝卖家，非常抱歉的告诉您，您还没有购买此服务或者登录信息已失效，如需继续使用请<a href='http://fuwu.taobao.com/serv/detail.htm?service_id=4545' target='_blank'>进入该服务</a>，谢谢！";
            Response.Redirect("buy.aspx?msg=" + HttpUtility.UrlEncode(msg));
            Response.End();
            return;
        }
        
        //过期判断
        if (!IsBuy(nick))
        {
            string msg = "尊敬的" + nick + "，非常抱歉的告诉您，只有VIP版本才能使用【客户关系营销】功能，如需继续使用请<a href='http://fuwu.taobao.com/ser/detail.htm?service_code=service-0-22904&frm=haoping' target='_blank'>购买高级会员服务</a>，谢谢！";
            Response.Redirect("buy.aspx?msg=" + HttpUtility.UrlEncode(msg));
            Response.End();
            return;
        }

        if (act == "msend")
        {
            SendCoupon();
            return;
        }

        BindData();
    }

    /// <summary>
    /// 判断该用户是否订购了该服务
    /// </summary>
    /// <param name="nick"></param>
    /// <returns></returns>
    private bool IsBuy(string nick)
    {
        string sql = "SELECT plus FROM TCS_ShopSession WHERE nick = '" + nick + "'";
        DataTable dt = utils.ExecuteDataTable(sql);
        if (dt.Rows.Count != 0)
        {
            string plus = dt.Rows[0][0].ToString();
            if (plus.IndexOf("crm") != -1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        else
        {
            return false;
        }
    }

    private void BindData()
    {
        //数据绑定
        DataTable dtCoupon = utils.ExecuteDataTable("SELECT * FROM TCS_Coupon WHERE nick = '" + nick + "' AND isdel = 0 ORDER BY startdate DESC");

        if (dtCoupon.Rows.Count <= 0)
        {
            Response.Write("<script>alert('请先创建1张淘宝优惠券才能可以给买家赠送优惠券！');window.location.href='../reviewnew/couponadd.aspx'</script>");
            Response.End();
        }

        couponstr = "<select name='couponid'>";
        for (int i = 0; i < dtCoupon.Rows.Count; i++)
        {
            couponstr += "<option value='" + dtCoupon.Rows[i]["taobaocouponid"].ToString() + "'>" + dtCoupon.Rows[i]["name"].ToString() + " - " + dtCoupon.Rows[i]["num"].ToString() + "元</option>";
        }
        couponstr += "</select>";

        string sql = string.Empty;
        sql = "SELECT * FROM TCS_Group WHERE nick = '" + nick + "' AND isdel = 0";
        DataTable dt = utils.ExecuteDataTable(sql);

        string appkey = "12159997";
        string secret = "614e40bfdb96e9063031d1a9e56fbed5";

        IDictionary<string, string> param = new Dictionary<string, string>();
        param.Add("page_size", "100");
        param.Add("current_page", "1");

        string result = Post("http://gw.api.taobao.com/router/rest", appkey, secret, "taobao.crm.groups.get", session, param);
        Regex reg = new Regex(@"<group>([\s\S]*?)</group>", RegexOptions.IgnoreCase);
        MatchCollection match = reg.Matches(result);

        for (int i = 0; i < match.Count; i++)
        {
            string str = match[i].Groups[0].ToString();
            string groupid = GetValueByProperty(str, "group_id");
            string groupName = GetValueByProperty(str, "group_name");
            string groupCount = GetValueByProperty(str, "member_count");

            //如果数据库内有记录则显示
            for (int j = 0; j < dt.Rows.Count; j++)
            {
                if (groupid == dt.Rows[j]["taobaoid"].ToString())
                {
                    //row["price"] = dt.Rows[j]["price"];
                    //row["priceend"] = dt.Rows[j]["priceend"];
                    //row["arealist"] = InitAreaStr(dt.Rows[j]["arealist"].ToString());
                    //row["actdate"] = dt.Rows[j]["actdate"];
                    //row["actdateend"] = dt.Rows[j]["actdateend"];
                    groupstr += "<option value='" + groupid + "' title='" + groupCount + "'>" + groupName + "</option>";
                }
            }
        }
    }


    public static string GetValueByProperty(string str, string prop)
    {
        Regex reg = new Regex(@"<" + prop + @">([^<]*)</" + prop + @">", RegexOptions.IgnoreCase);
        if (reg.IsMatch(str))
        {
            return reg.Match(str).Groups[1].ToString();
        }
        else
        {
            return "";
        }
    }

    /// <summary>
    /// 保存设置
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Button1_Click(object sender, EventArgs e)
    {
        SendCoupon();
    }

    private void SendCoupon()
    {
        //执行优惠券赠送行为
        string appkey = "12159997";
        string secret = "614e40bfdb96e9063031d1a9e56fbed5";
        string sql = string.Empty;
        string result = string.Empty;
        string typ = utils.NewRequest("typ", utils.RequestType.Form);

        string couponid = utils.NewRequest("couponid", utils.RequestType.Form);
        string buynick = "";// this.txtBuyerNick.Text;

        if (mpage == "")
        {
            mpage = "1";
        }
        else
        {
            typ = mgroupid;
            couponid = mcouponid;
        }

        //获取淘宝优惠券ID
        sql = "SELECT guid FROM TCS_Coupon WHERE taobaocouponid = '" + couponid + "'";
        string guid = utils.ExecuteString(sql);

        string condition = string.Empty;
        IDictionary<string, string> param = new Dictionary<string, string>();

        int index = 0;
        int err = 0;
        string errtext = string.Empty;

        //总数量
        if (mcount != "")
        {
            index = index + int.Parse(mcount);
        }

        for (int i = int.Parse(mpage); i < 9999; i++)
        {
            //获取买家分组信息
            param = new Dictionary<string, string>();
            param.Add("page_size", "100");
            param.Add("current_page", i.ToString());
            param.Add("relation_source", "1");
            param.Add("order", "lastest_time");
            param.Add("sort", "desc");
            if (typ == "1" || typ == "2" || typ == "3" || typ == "4")
            {
                param.Add("grade", typ);
            }
            else if (typ == "")
            {
                //所有会员
            }
            else
            {
                param.Add("group_id", typ);
            }

            result = Post("http://gw.api.taobao.com/router/rest", appkey, secret, "taobao.crm.members.search", session, param);
            Regex reg = new Regex(@"<crm_member>([\s\S]*?)</crm_member>", RegexOptions.IgnoreCase);
            MatchCollection match = reg.Matches(result);

            //如果没有了则退出
            if (match.Count == 0)
            {
                break;
            }

            string buynickList = string.Empty; ;

            //循环更新
            for (int j = 0; j < match.Count; j++)
            {

                //sql = "SELECT * FROM TCS_Customer b WHERE b.nick = '" + nick + "' " + condition + "";
                //DataTable dt = utils.ExecuteDataTable(sql);
                //for (int i = 0; i < dt.Rows.Count; i++)
                //{
                string strCus = match[j].Groups[0].ToString();
                buynick = GetValueByProperty(strCus, "buyer_nick");

                if (IsBlack(nick, buynick))
                {
                    //黑名单 跳过继续
                    continue;
                }

                buynickList += "," + buynick;
            }

            if (buynickList.Length > 1)
            {
                buynickList = buynickList.Substring(1, buynickList.Length - 1);

                //批量赠送
                param = new Dictionary<string, string>();
                param.Add("coupon_id", couponid);
                param.Add("buyer_nick", buynickList);
                try
                {
                    result = Post("http://gw.api.taobao.com/router/rest", appkey, secret, "taobao.promotion.coupon.send", session, param);
                    Regex regCoupon = new Regex(@"<buyer_nick>([^<]*)</buyer_nick><coupon_number>([^<]*)</coupon_number>", RegexOptions.IgnoreCase);
                    MatchCollection matchCoupon = regCoupon.Matches(result);

                    for (int j = 0; j < matchCoupon.Count; j++)
                    {
                        index++;
                        string number = matchCoupon[j].Groups[2].ToString();
                        buynick = matchCoupon[j].Groups[1].ToString();

                        ////赠送优惠券
                        //sql = "INSERT INTO TCS_CouponSend (" +
                        //                    "nick, " +
                        //                    "guid, " +
                        //                    "buynick, " +
                        //                    "taobaonumber " +
                        //                " ) VALUES ( " +
                        //                    " '" + nick + "', " +
                        //                    " '" + guid + "', " +
                        //                    " '" + buynick + "', " +
                        //                    " '" + number + "' " +
                        //                ") ";
                        ////Response.Write(sql);
                        ////Response.End();
                        //utils.ExecuteNonQuery(sql);

                        ////更新优惠券已经赠送数量
                        //sql = "UPDATE TCS_Coupon SET used = used + 1 WHERE guid = '" + guid + "'";
                        //utils.ExecuteNonQuery(sql);

                        sql = "[InsertCouponSendLog] '" + nick + "','" + guid + "','" + buynick + "','" + number + "'";
                        utils.ExecuteNonQuery(sql);
                    }
                    ////如果失败
                    //if (!regCoupon.IsMatch(result))
                    //{
                    //    string errtexttemp = new Regex(@"<reason>([^<]*)</reason>", RegexOptions.IgnoreCase).Match(result).Groups[1].ToString();
                    //    if (errtexttemp.Length == 0)
                    //    {
                    //        errtexttemp = new Regex(@"<sub_msg>([^<]*)</sub_msg>", RegexOptions.IgnoreCase).Match(result).Groups[1].ToString();
                    //    }
                    //    if (errtext.IndexOf(errtexttemp) == -1)
                    //    {
                    //        errtext += "|" + errtexttemp;
                    //    }
                    //    err++;
                    //    //string err = new Regex(@"<reason>([^<]*)</reason>", RegexOptions.IgnoreCase).Match(result).Groups[1].ToString();
                    //    //Response.Write("<script>alert('【系统错误】：" + err + "，请稍后再试或者联系客服人员！');window.location.href='msgsend.aspx';</script>");
                    //}
                    //else
                    //{
                    //    index++;
                    //    string number = matchCoupon[0].Groups[1].ToString();

                    //    //赠送优惠券
                    //    sql = "INSERT INTO TCS_CouponSend (" +
                    //                        "nick, " +
                    //                        "guid, " +
                    //                        "buynick, " +
                    //                        "taobaonumber " +
                    //                    " ) VALUES ( " +
                    //                        " '" + nick + "', " +
                    //                        " '" + guid + "', " +
                    //                        " '" + buynick + "', " +
                    //                        " '" + number + "' " +
                    //                    ") ";
                    //    //Response.Write(sql);
                    //    //Response.End();
                    //    utils.ExecuteNonQuery(sql);

                    //    //更新优惠券已经赠送数量
                    //    sql = "UPDATE TCS_Coupon SET used = used + 1 WHERE guid = '" + guid + "'";
                    //    utils.ExecuteNonQuery(sql);
                    //}

                    //Response.Write(result);
                }
                catch { }
            }

            if (match.Count != 0)
            {
                //跳转到下页继续赠送，防止中断！
                string url = "msgsend.aspx?act=msend&mcouponid=" + couponid.ToString() + "&mpage=" + (int.Parse(mpage) + 1).ToString() + "&mgroupid=" + typ.ToString() + "&mcount=" + index.ToString();
                Response.Write("优惠券赠送中，目前已赠送数量【" + index.ToString() + "】张，请您耐心稍等......<script>function redirect(){window.location.href='" + url + "';} setTimeout('redirect()', 2000);</script>");
                Response.End();
                return;
            }
        }

        //if (err == 0)
        //{
        Response.Write("<script>alert('赠送完毕！');window.location.href='../reviewnew/couponsend.aspx';</script>");
        //}
        //else
        //{
        //    Response.Write("<script>alert('赠送完毕，成功赠送" + index.ToString() + "张，失败" + err.ToString() + "张，失败原因" + errtext + "！');window.location.href='../reviewnew/couponsend.aspx';</script>");
        //}
        Response.End();
    }

    public bool IsBlack(string nick, string buynick)
    {
        string sql = "SELECT COUNT(*) FROM TCS_BlackListGift WHERE nick = '" + nick + "' AND buynick = '" + buynick + "'";
        string count = utils.ExecuteString(sql);
        if (count == "0")
        {
            return false;
        }
        else
        {
            return true;
        }
    }

    #region TOP API
    /// <summary> 
    /// 给TOP请求签名 API v2.0 
    /// </summary> 
    /// <param name="parameters">所有字符型的TOP请求参数</param> 
    /// <param name="secret">签名密钥</param> 
    /// <returns>签名</returns> 
    protected static string CreateSign(IDictionary<string, string> parameters, string secret)
    {
        parameters.Remove("sign");
        IDictionary<string, string> sortedParams = new SortedDictionary<string, string>(parameters);
        IEnumerator<KeyValuePair<string, string>> dem = sortedParams.GetEnumerator();
        StringBuilder query = new StringBuilder(secret);
        while (dem.MoveNext())
        {
            string key = dem.Current.Key;
            string value = dem.Current.Value;
            if (!string.IsNullOrEmpty(key) && !string.IsNullOrEmpty(value))
            {
                query.Append(key).Append(value);
            }
        }
        query.Append(secret);
        MD5 md5 = MD5.Create();
        byte[] bytes = md5.ComputeHash(Encoding.UTF8.GetBytes(query.ToString()));
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < bytes.Length; i++)
        {
            string hex = bytes[i].ToString("X");
            if (hex.Length == 1)
            {
                result.Append("0");
            }
            result.Append(hex);
        }
        return result.ToString();
    }
    /// <summary> 
    /// 组装普通文本请求参数。 
    /// </summary> 
    /// <param name="parameters">Key-Value形式请求参数字典</param> 
    /// <returns>URL编码后的请求数据</returns> 
    protected static string PostData(IDictionary<string, string> parameters)
    {
        StringBuilder postData = new StringBuilder();
        bool hasParam = false;
        IEnumerator<KeyValuePair<string, string>> dem = parameters.GetEnumerator();
        while (dem.MoveNext())
        {
            string name = dem.Current.Key;
            string value = dem.Current.Value;
            // 忽略参数名或参数值为空的参数 
            if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(value))
            {
                if (hasParam)
                {
                    postData.Append("&");
                }
                postData.Append(name);
                postData.Append("=");
                postData.Append(Uri.EscapeDataString(value));
                hasParam = true;
            }
        }
        return postData.ToString();
    }
    /// <summary> 
    /// TOP API POST 请求 
    /// </summary> 
    /// <param name="url">请求容器URL</param> 
    /// <param name="appkey">AppKey</param> 
    /// <param name="appSecret">AppSecret</param> 
    /// <param name="method">API接口方法名</param> 
    /// <param name="session">调用私有的sessionkey</param> 
    /// <param name="param">请求参数</param> 
    /// <returns>返回字符串</returns> 
    public static string Post(string url, string appkey, string appSecret, string method, string session,
    IDictionary<string, string> param)
    {
        #region -----API系统参数----
        param.Add("app_key", appkey);
        param.Add("method", method);
        param.Add("session", session);
        param.Add("timestamp", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
        param.Add("format", "xml");
        param.Add("v", "2.0");
        param.Add("sign_method", "md5");
        param.Add("sign", CreateSign(param, appSecret));
        #endregion
        string result = string.Empty;
        #region ---- 完成 HTTP POST 请求----
        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
        req.Method = "POST";
        req.KeepAlive = true;
        req.Timeout = 300000;
        req.ContentType = "application/x-www-form-urlencoded;charset=gb2312";
        byte[] postData = Encoding.UTF8.GetBytes(PostData(param));
        Stream reqStream = req.GetRequestStream();
        reqStream.Write(postData, 0, postData.Length);
        reqStream.Close();
        HttpWebResponse rsp = (HttpWebResponse)req.GetResponse();
        Encoding encoding = Encoding.GetEncoding(rsp.CharacterSet);
        Stream stream = null;
        StreamReader reader = null;
        stream = rsp.GetResponseStream();
        reader = new StreamReader(stream, encoding);
        result = reader.ReadToEnd();
        if (reader != null) reader.Close();
        if (stream != null) stream.Close();
        if (rsp != null) rsp.Close();
        #endregion
        return Regex.Replace(result, @"[\x00-\x08\x0b-\x0c\x0e-\x1f]", "");
    }
    #endregion
}